package com.base.common.enums;

import java.util.regex.Pattern;

/**
 * @author wsz
 * @create 2023/4/21
 */
public enum TimeRegexEnum {

    TIME_PATTERN_1("\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$","yyyy-MM-dd HH:mm:ss"),
    TIME_PATTERN_2("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$","yyyy-MM-dd HH:mm"),
    TIME_PATTERN_3("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd"),
    TIME_PATTERN_4("^\\d{4}-\\d{1,2}$", "yyyy-MM"),
    TIME_PATTERN_5("^\\d{2}:\\d{2}:\\d{2}$", "HH:mm:ss"),
    TIME_PATTERN_6("^\\d{2}/\\d{2}/\\d{4}$", "dd/MM/yyyy"),
    TIME_PATTERN_7("^\\d{2}-\\d{2}-\\d{4}$", "dd/MM/yyyy");


    private final String regex;
    private final String formatter;
    private final Pattern pattern;

    TimeRegexEnum(String regex, String formatter) {
        this.regex = regex;
        this.formatter = formatter;
        this.pattern = Pattern.compile(regex);
    }

    public String getRegex() {
        return regex;
    }

    public String getFormatter() {
        return formatter;
    }

    public static String tryGetFormatter(String text) {
        for (TimeRegexEnum value : values()) {
            if (value.pattern.matcher(text).matches()) {
                return value.formatter;
            }
        }
        return null;
    }
}
